package com.hd.crdao.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.hd.crdao.R;
import com.hd.crdao.activity.PayResultActivity;
import com.hd.crdao.activity.PaySuccessActivity;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {



    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID,false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }


    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        wxPayResult(resp.errCode);
        finish();
    }

    /**
     * 微信回调页面返回
     * @param wxcode 微信回调参数
     */
    public void wxPayResult(int wxcode){
        switch (wxcode) {
            case 0:
//                MyToast.show("支付成功");
                String domainPayinfo = SPUtils.getString("domainPayinfo", "");
                String payinfo = SPUtils.getString("payinfo", "");
                if(!TextUtils.isEmpty(payinfo)){
                    String[] split = payinfo.split(",");
                    //跳转到支付结果界面
                    Intent i = new Intent(UIUtils.getContext(),PayResultActivity.class);
                    i.putExtra("datatime",split[3]);
                    i.putExtra("plorder",split[2]);
                    i.putExtra("plMethod",split[0]);
                    i.putExtra("vin",split[1]);
                    startActivity(i);
                }
                if(!TextUtils.isEmpty(domainPayinfo)){
                    Intent i = new Intent(UIUtils.getContext(),PaySuccessActivity.class);
                    i.putExtra("domainPrice",domainPayinfo);
                    i.putExtra("payType","微信支付");
                    startActivity(i);
                }
                break;
            case -2:
                MyToast.show("取消支付");
                break;
            default:
                MyToast.show("支付失败:"+wxcode);
                break;
        }
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:

                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:

                break;
            default:
                break;
        }
    }
}